package com.mkvld.appietijdenv2;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;

import com.mkvld.appietijdenv2.activities.ActV9;
import com.mkvld.appietijdenv2.cloud.SyncServiceV9;
import com.mkvld.appietijdenv2.data.Shift;
import com.mkvld.appietijdenv2.data.Shift.ShiftType;
import com.mkvld.appietijdenv2.databases.ShiftDatabase;
import com.mkvld.appietijdenv2.dialogs.DialNewShiftV9;
import com.mkvld.appietijdenv2.fragments.FragShiftsV9;
import com.mkvld.appietijdenv2.views.ViewShift;

public class FragShiftsV14 extends FragShiftsV9 {
	List<ViewShift> selectedViews;
	private int week;
	private boolean singleWeek = true;
	ActionMode actionMode;
	
	private Shift[] getSelectedShifts() {
		Shift[] shifts = new Shift[selectedViews.size()];
		for (int i = 0; i < shifts.length; i++) {
			ViewShift vs = selectedViews.get(i);
			shifts[i] = new Shift(ShiftType.PRODUCTIVE, getYear(), getPeriod() * 4 + vs.getWeek(), vs.getDay(), vs.getStart(), vs.getEnd(), -1);
		}
		return shifts;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		selectedViews = new ArrayList<ViewShift>();
	}
	
	@Override
	protected void showNewShiftDialog(int year, int week, int day) {
		DialNewShiftV14.showNew(new DialNewShiftV14(), getActivity(), year, week, day);
		if (actionMode != null) {
			actionMode.finish();
		}
	}

	@Override
	public void shiftSelected(ViewShift vs) {
		if (selectedViews.size() == 0) {
			selectedViews.add(vs);
			week = vs.getWeek();
			singleWeek = true;
			actionMode = getActivity().startActionMode(callback);
			
			vs.setActivated(true);
		} else {
			if (selectedViews.contains(vs)) { // if the item is already selected
				selectedViews.remove(vs);
				vs.setActivated(false);
				if (selectedViews.size() == 0)
					actionMode.finish();
				else {
					actionMode.invalidate();
					if (!singleWeek) { // if different week were selected before recheck
						week = selectedViews.get(0).getWeek();
						singleWeek = true;
						for (ViewShift vs2 : selectedViews) {
							if (vs2.getWeek() != week) {
								singleWeek = false;
								break;
							}
							
						}
					}
				}
			} else {
				selectedViews.add(vs);
				if (week != vs.getWeek())
					singleWeek = false;
				vs.setActivated(true);
				actionMode.invalidate();
			}
		}
	}
	
	
	
	ActionMode.Callback callback = new ActionMode.Callback() {

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			if (item.getItemId() == R.id.fsa_add) {
				DialNewShiftV9.showNew(new DialNewShiftV14(), getActivity(), getYear(), 
						getPeriod() * 4 + selectedViews.get(0).getWeek(), 
						selectedViews.get(0).getDay());
			} else if (item.getItemId() == R.id.fsa_edit) {
				ViewShift vs = selectedViews.get(0);
				DialNewShiftV9.showEdit(new DialNewShiftV14(), getActivity(), 
						new Shift(ShiftType.PRODUCTIVE, getYear(), getPeriod() * 4 + vs.getWeek(), 
								vs.getDay(), vs.getStart(), vs.getEnd(), -1));
			} else if (item.getItemId() == R.id.fsa_delete) {
				for (ViewShift vs : selectedViews) {
					ShiftDatabase.db.deleteShift(new Shift(ShiftType.PRODUCTIVE, getYear(), getPeriod() * 4 + vs.getWeek(), vs.getDay(), vs.getStart(), vs.getEnd(), -1));
					SyncServiceV9.deleteCloudShift(getActivity(), new Shift(ShiftType.PRODUCTIVE, getYear(), getPeriod() * 4 + vs.getWeek(), vs.getDay(), vs.getStart(), vs.getEnd(), -1));
				}
			} else if (item.getItemId() == R.id.fsa_copy) {
				DialCopyMoveV14.showCopy((ActV9) getActivity(), getSelectedShifts(), singleWeek);
			} else if (item.getItemId() == R.id.fsa_move) {
				DialCopyMoveV14.showMove((ActV9) getActivity(), getSelectedShifts(), singleWeek);
			}
			mode.finish();
			((ActV9)getActivity()).update();
			return true;
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			mode.getMenuInflater().inflate(R.menu.fs_actionmode, menu);
			return true;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			for (ViewShift vs : selectedViews) {
				vs.setActivated(false);
			}
			selectedViews.clear();
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			menu.setGroupVisible(R.id.fsa_singleitem, selectedViews.size() == 1);
			menu.setGroupVisible(R.id.fsa_singleweek, singleWeek);
			return true;
		}
		
	};

}
